package chinese.admin.controllers;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import chinese.admin.constants.MemuEnum;
import chinese.admin.dao.ChnArticleDAO;
import chinese.admin.dao.ChnAuthorDAO;
import chinese.admin.domain.ChnArticle;
import chinese.admin.domain.ChnAuthor;
import chinese.constant.enums.ChnArticleStatusEnum;
import cj.utils.DateUtils;

/**
 * <p>实体类</p>
 * <p>Table: ChnAuthor</p>
 *
 */
@Path("chnauthor")
public class ChnAuthorController {
	@Autowired
	private ChnAuthorDAO chnauthorDao;
	@Autowired
	private ChnArticleDAO chnarticleDao;
	
	@Get("show")
	public String show(Model model){
		Date startDate = DateUtils.parse("2017-1-1", DateUtils.DATE_SMALL_STR);
		Integer startYear = Integer.valueOf(DateUtils.getDomianOfDate(startDate, Calendar.YEAR));
		Date nowDate = new Date();
		Integer year = Integer.valueOf(DateUtils.getDomianOfDate(nowDate, Calendar.YEAR));
		List<Integer> years = new ArrayList<Integer>();
		for(Integer i = startYear; i <= year; i++){
			years.add(i);
		}
		int months[] = {0,1,2,3,4,5,6,7,8,9,10,11};
		model.add("Years", years);
		model.add("Months", months);
		model.add("MemuEnum", MemuEnum.chn_allRank);
		return "/views/chn/authorList.vm";
	}
	
	@Get("weekShow")
	public String weekShow(Model model){
		Date startDate = DateUtils.parse("2016-1-1", DateUtils.DATE_SMALL_STR);
		Integer startYearForWeek = Integer.valueOf(DateUtils.getYearForWeek(startDate));
		Date nowDate = new Date();
		Integer yearForWeek = Integer.valueOf(DateUtils.getYearForWeek(nowDate));
		int currentWeek = DateUtils.getDomianOfDate(nowDate, Calendar.WEEK_OF_YEAR);
		Map<Integer, Integer> yearWeeks = new TreeMap<Integer, Integer>(new Comparator<Integer>() {
			public int compare(Integer o1, Integer o2) {
				return o2.compareTo(o1);
			}
		});
		for(Integer i = startYearForWeek; i <= yearForWeek; i++){
			if(i.intValue()!=yearForWeek.intValue()){
				yearWeeks.put(i, DateUtils.getMaxWeekOfYear(i));
			}
			else{
				yearWeeks.put(i, currentWeek);
			}
		}
		
		model.add("YearWeek", yearWeeks);
		model.add("MemuEnum", MemuEnum.chn_weekRank);
		return "/views/chn/authorList.vm";
	}
	
	@Post("list")
	public String list(ChnAuthor chnauthor){
		JSONObject jo = new JSONObject();
		
		List<ChnAuthor> objs = chnauthorDao.query(chnauthor);
		int size = chnauthorDao.querySize(chnauthor);
		jo.put("data", objs);
		jo.put("draw", chnauthor.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		jo.put("objs", objs);
		return "@" + jo.toString();
	}
	
	@Get("articles")
	public String articles(Model model, @Param("authorId")int authorId){
		List<Map> articleTypes = chnarticleDao.articleTypeAsEnum();
		model.add("ArticleType", articleTypes);
		model.add("authorId",authorId);
		return "/views/chn/articleOfAuthor.vm";
	}
	
	@Post("articleOfAthour")
	public String articleOfAthour(ChnArticle article){
		article.setStatus(ChnArticleStatusEnum.ALLOWED.getValue());
		
		JSONObject jo = new JSONObject();
		
		List<ChnArticle> objs = chnarticleDao.queryArticles(article);
		int size = chnarticleDao.queryArticlesSize(article);
		jo.put("data", objs);
		jo.put("draw", article.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		jo.put("objs", objs);
		return "@" + jo.toString();
	}
	
	@Get("invent")
	public String invent(Model model){
		return "chnauthorDetail.vm";
	}
	
	@Post("create")
	public String create(ChnAuthor chnauthor){
		JSONObject jo = new JSONObject();
		
		chnauthorDao.create(chnauthor);
		return "@" + jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model){
		return "chnauthorEdit.vm";
	}
	
	@Post("update")
	public String update(ChnAuthor chnauthor){
		JSONObject jo = new JSONObject();
		
		chnauthorDao.update(chnauthor);
		return "@" + jo.toString();
	}
	
	@Get("delete")
	public String delete(@Param("id")int id){
		JSONObject jo = new JSONObject();
	
		chnauthorDao.delete(id);
		return "@" + jo.toString();
	}

}